package cn.itcast.reggie.service.impl;

import cn.itcast.reggie.common.MyIllegalArgumentException;
import cn.itcast.reggie.mapper.CategoryMapper;
import cn.itcast.reggie.pojo.Category;
import cn.itcast.reggie.pojo.SetmealDish;
import cn.itcast.reggie.service.CategoryService;
import cn.itcast.reggie.service.SetmealDishService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void delete(String[] ids) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.in("id",ids);
        wrapper.eq("status",1);
        int count = count(wrapper);
        if(count > 0) {
            throw new MyIllegalArgumentException("删除的套餐，正在售卖");
        }

        //根据id删除套餐
        removeByIds(Arrays.asList(ids));

        //根据套餐id删除，套餐和菜品关系
        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("setmeal_id",ids);
        setmealDishService.remove(queryWrapper);
    }
}